package com.lkd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.lkd.dao.RegionDao;
import com.lkd.entity.NodeEntity;
import com.lkd.entity.RegionEntity;
import com.lkd.exception.LogicException;
import com.lkd.service.NodeService;
import com.lkd.service.RegionService;
import com.lkd.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class RegionServiceImpl extends ServiceImpl<RegionDao, RegionEntity> implements RegionService {



    @Autowired
    private NodeService nodeService;
    @Autowired
    private RegionService regionService;

    /**
     * 分页搜索
     *
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Pager<RegionEntity> getRegionByPage(Long pageIndex, Long pageSize, String name) {
        //1.判断前端传来的数据
        if (ObjectUtils.isEmpty(pageIndex) || pageIndex <= 0) {
            pageIndex = Pager.DEFAULT_PAGE_INDEX;
        }
        if (ObjectUtils.isEmpty(pageSize) || pageSize <= 0) {
            pageSize = Pager.DEFAULT_PAGE_INDEX;
        }

        //2.创建分页对象
        Page<RegionEntity> page = new Page<>(pageIndex, pageSize);

        //3.模糊查询,创建查询对象
        LambdaQueryWrapper<RegionEntity> queryWrapper = new LambdaQueryWrapper<>();
        //判断模糊查询的内容不能为空或者空字符串
        if (!Strings.isNullOrEmpty(name)) {
            queryWrapper.like(RegionEntity::getName, name);
        }

        //4.封装数据,返回数据
        Page<RegionEntity> pages = page(page, queryWrapper);
        Pager<RegionEntity> pager = Pager.build(pages);
        return pager;
    }

    /**
     * 增加区域
     *
     * @param regionEntity RegionEntity 区域实体类
     * @return
     */
    @Override
    public Boolean addRegion(RegionEntity regionEntity) {
        //1.判断关键数据(前端传来的数据)
        if (Strings.isNullOrEmpty(regionEntity.getName()) ||
                Strings.isNullOrEmpty(regionEntity.getRemark())
        )//抛出异常
        {
            throw new LogicException("关键数据与接口不匹配");
        }
        //2.判断数据业务
        //3.封装数据,返回数据
        boolean result = regionService.save(regionEntity);
        return result;
    }



    /**
     * 修改区域信息
     *
     * @param id
     * @param regionEntity RegionEntity 区域实体类
     * @return
     */
    @Override
    public Boolean updateRegion(Long id, RegionEntity regionEntity) {
        //1.判断关键数据
        if (ObjectUtils.isEmpty(id) ||
                ObjectUtils.isEmpty(regionEntity.getName()) ||
                ObjectUtils.isEmpty(regionEntity.getRemark())
        ){
            throw new LogicException("关键数据与接口不匹配");
        }
        //2.判断业务数据-判断区域信息是否存在
        LambdaQueryWrapper<RegionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RegionEntity::getId,regionEntity.getId());
        int count = this.count(queryWrapper);
        //count小于1说明数据不存在
        if (count<1){
            throw new LogicException("区域消息不存在");
        }
        //3.封装数据,返回数据
        boolean result = this.updateById(regionEntity);
        return result;
    }

    /**
     *  删除区域信息
     * @param regionId Long 区域id值
     * @return boolean  类型值
     */
    @Override
    public Boolean deleteRegion(String regionId) {
        //1.判断关键数据
        if (ObjectUtils.isEmpty(regionId)){
            throw new LogicException("关键数据与接口不匹配");
        }
        //2.判断业务数据-判断区域信息是否存在
        LambdaQueryWrapper<RegionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RegionEntity::getId,regionId);
        int count = this.count(queryWrapper);
        //count小于1说明数据不存在
        if (count<1){
            throw new LogicException("区域消息不存在");
        }
        //3.封装数据,返回数据
        boolean result = this.removeById(regionId);
        return result;
    }


}
